﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace WindowsGame1
{
    class inGameScene : Scene
    {
        //Actor tipo;
        //Helpers helper = new Helpers();
        //-Lista de actores
        List<Actor> Actors = new List<Actor>();
        List<Actor> actorsToAdd = new List<Actor>();
        List<Actor> actorsToRemove = new List<Actor>();
        //------------------
        InputManager Input = new InputManager();

        ContentManager Content;
        CollisionManager Collisions = new CollisionManager();

        public inGameScene(ContentManager content){
            Content = content;
            //createActor("spritesheet");
            Actor x = new Actor(Content, "spritesheet");
            x.SetFrames(8, 8);
            int[] frames = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,28,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59};
            x.CreateAnimation("unica",frames);
            x.SetAnimation("unica");
            x.SetDirection(1, 1);
            Actors.Add(x);

            Collisions.AddToListA(x);

            
            //tipo = new Actor();
            //tipo.tex = Content.Load<Texture2D>("spritesheet");
            //tipo.position = new Vector2(50, 50);
            //tipo.direction = new Vector2(1, 1);
        }
        public void createActor(String texture)
        {
            Actor a = new Actor(Content, texture);

        }

        public override void update(int dTime)
        {
              //tipo.update(dTime);
            foreach (Actor a in Actors)
            {
                a.update(dTime);
            }

            Collisions.update();

            foreach (Actor a in Actors)
            {
                if (a.readyToexit)
                {
                    actorsToRemove.Add(a);
                }
            }

            foreach (Actor a in actorsToAdd)
            {
                Actors.Add(a);
            }

            foreach (Actor a in actorsToRemove)
            {
                Actors.Remove(a);

            }
            actorsToAdd.Clear();
            actorsToRemove.Clear();
            
            base.update(dTime);
        }

        public override void draw(SpriteBatch sb)
        {
            foreach (Actor a in Actors) { 
                a.Draw(sb);
            } 
            
            //sb.Draw(tipo.tex, tipo.position, Color.White);ç
            //Helpers.drawLine(new Vector2(10, 10), new Vector2(200, 200));
            //Helpers.drawRect(new Vector2(300,300), new Vector2(400,400));
            base.draw(sb);
        }

        public override void handleInput(InputManager Input)
        {
            if (Input.isNewMouseClick())
            {
                Actor x = new Actor(Content, "spritesheet");
                x.SetFrames(8, 8);
                int[] frames = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 28, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59 };
                x.CreateAnimation("unica", frames);
                x.SetAnimation("unica");
                x.SetDirection(1, -1);
                actorsToAdd.Add(x);

                Collisions.AddToListB(x);

            }
            base.handleInput(Input);
        }
    }
}
